#pragma once
/**
* Omvandlar Bin <-> Dec.
*/
#include "Bitstr.h"

class Omvandla
{
private:
	Bitstr derp;
public:

	Omvandla(void)
	{
	}

	/** Decimal -> Bin&auml;r.
	* Omvandlar ett decimalt heltal till en bin&auml;r str&auml;ng.
	*/
	void tal_str(int tal, string &str, int nrbits)
	{
		str.resize(nrbits);
		if(tal < 0)
		{
			tal = abs(tal);
			for (int i = nrbits-1; i >= 0; i--)
			{
				str[i] =tal%2 + '0';
				tal = tal/2;
			}
			derp.twokomp(str);
		}
		else
		{
			for (int i = nrbits-1; i >= 0; i--)
			{
				str[i] =tal%2 + '0';
				tal = tal/2;
			}
		}
	}

	/** Bin&auml;rt -> Decimal.
	* Omvandlar fr&aring;n bin&auml;r str&auml;ng till ett decimalt heltal.
	*/
	int str_tal(string &str)
	{
		int sl = str.length();
		int tal = 0;
		if(str[0] == '1')
		{
			derp.twokomp(str);
			for (int i = sl-1; i >= 0; i--)
			{
				tal += (str[i]-'0') * pow(2,sl-i-1);
			}
			tal *= -1;
		}
		else
		{
			for (int i = sl-1; i >= 0; i--)
			{
				tal += (str[i]-'0') * pow(2,sl-i-1);
			}
		}
		return tal;
	}



	~Omvandla(void)
	{
	}
};

